//index.js
//获取应用实例
var config = require('../../config/config.js');
const app = getApp()

Page({
  data: {
    motto: '水加加欢迎您',
    userInfo: {},
    hasUserInfo: false,
    avatarurl:'',
    nickname:'',
    clickto:'点击图像进入',
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },

  onLoad: function () {
    if (wx.getStorageSync('work_info')) {
      wx.redirectTo({
        url: '../dingdan/dingdan',
      })
      return
    }
    if (wx.getStorageSync('member_info')) {
      wx.switchTab({
        url: '../takeWater/takeWater',
      })
      return
    }
 
  },
  getUserInfo: function (e) {
    var that = this;
    var member_info = (wx.getStorageSync('member_info'))
    if (member_info) {
      wx.getUserInfo({
        success: function (res) {
          that.setData({
            nickname: member_info.nickname,
            avatarurl: member_info.avatarurl,
          })
        },
        fail: function () {
          wx.showModal({
            title: '提示',
            content: '授权后才可访问',
          })
        },
        complete: function () {
          console.log("获取用户信息完成！")
        }
      })
    } else {
      wx.login({
        success: function (res) {
          if (res.code) {
            wx.getUserInfo({
              withCredentials: true, //是否带上登录信息
              success: function (res_user) {
                wx.request({
                  //后台接口地址
                  url: config.wxlogin,
                  data: {  //加密处理
                    code: res.code,
                    encryptedData: res_user.encryptedData,
                    iv: res_user.iv
                  },
                  method: 'GET',
                  header: {
                    'content-type': 'application/json'
                  },
                  success: function (res) {
                    // this.globalData.userInfo = JSON.parse(res.data);
                    that.setData({
                      nickname: res.data.data.nickname,
                      avatarurl: res.data.data.avatarurl,
                      // hasUserInfo: true
                    })
                    wx.setStorageSync('member_info', res.data.data);
                    wx.switchTab({
                      url: '../takeWater/takeWater'
                    })
                  }
                })
              }, fail: function () {
                wx.showModal({
                  title: '警告通知',
                  content: '您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。',
                  success: function (res) {
                    if (res.confirm) {
                      wx.openSetting({  //调起客户端小程序设置界面，返回用户设置的操作结果。
                        success: (res) => {
                          if (res.authSetting["scope.userInfo"]) {//如果用户重新同意了授权登录
                          debugger
                            wx.login({
                              success: function (res_login) {
                                if (res_login.code) {
                                  wx.getUserInfo({
                                    withCredentials: true,
                                    success: function (res_user) {
                                      wx.request({
                                        url: config.wxlogin,
                                        data: {
                                          code: res_login.code,
                                          encryptedData: res_user.encryptedData,
                                          iv: res_user.iv
                                        },
                                        method: 'GET',
                                        header: {
                                          'content-type': 'application/json'
                                        },
                                        success: function (res) {
                                          that.setData({
                                            nickname: res.data.nickname,
                                            avatarurl: res.data.avatarurl,
                                            // hasUserInfo: true
                                          })
                                          wx.setStorageSync('member_info', res.data.data);
                                        
                                        }
                                      })
                                    }
                                  })
                                }
                              }
                            });
                          }
                        }, fail: function (res) {
                        }
                      })

                    }
                  }
                })
              }, complete: function (res) {
              }
            })
          }
        }
      })

    }
  },
  //事件处理函数
  // bindViewTap: function () {
  //   wx.switchTab({
  //     url: '../takeWater/takeWater'
  //   })
  // },
})
